package com.shanghaionstar.vehicle_ownerlife;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.PreferencesUtls;
import com.shanghaionstar.R;
import com.shanghaionstar.alarm.AlarmActionType;
import com.shanghaionstar.alarm.AlarmProxy;
import com.shanghaionstar.utils.FinalConstants;
import com.shanghaionstar.utils.NotificationUtil;

/**
 * 车检提醒
 * Created by Yonglang Hong on 2017/1/5.
 */
public class InspectHelper {


    /**
     * 设置行驶证日期和预期年检日期
     * @param context
     * @param licenseDay
     */
    public static void setLicenseInspectDateAndAlarm(Context context,String licenseDay){
        if (CommonUtil.isNull(licenseDay)) {
            setBuyCarDate(context, "");
            setInspectDate(context, "");
            //此时取消所有闹钟
            new AlarmProxy(context).cancel(AlarmActionType.VEHICLE_INSPECTION_ACTION);
            return;
        }
        //存储行驶证日期
        setBuyCarDate(context,licenseDay);
        //设置年检日期
        setInspectDate(context,licenseDay);
        //设置闹钟
        new AlarmProxy(context).alarm(AlarmActionType.VEHICLE_INSPECTION_ACTION);
    }

    /**
     * 设置年检日期
     * @param context
     */
    public static void setInspectDate(Context context,String licenseDay){
        if (CommonUtil.isNull(licenseDay)) {
            PreferencesUtls.getInstance(context).putString(FinalConstants.SharedPreferencesConfig.INSPECT_DATE, "");
            return;
        }
        //购车时间在当前日期之后
        String inspectDate;
        if (DateHelper.nowIsBefore(licenseDay)) {
            inspectDate = DateHelper.updateDateYear(licenseDay,2);
        } else {
            //购买日期在六年之内，建议每两年年检一次
            if (DateHelper.isDateInScope(licenseDay, 6)) {
                inspectDate = formatDate(licenseDay,2);
            } else { //从第六年开始则建议每年年检一次
                inspectDate = formatDate(licenseDay,1);
            }
        }
        PreferencesUtls.getInstance(context).putString(FinalConstants.SharedPreferencesConfig.INSPECT_DATE,inspectDate);
    }

    /**
     * 根据规则改变日期
     * @param licenseDay
     * @param offSet
     * @return
     */
    private static String formatDate(String licenseDay, int offSet) {
        Log.d("formatDate", licenseDay);
        if (DateHelper.nowIsBefore(licenseDay)) {   //日期
            return licenseDay;
        } else {
            licenseDay = DateHelper.updateDateYear(licenseDay,offSet);
            return formatDate(licenseDay,offSet);
        }
    }

    /**
     * 设置购车日期
     * @param context
     */
    public static void setBuyCarDate(Context context,String licenseDay){
        PreferencesUtls.getInstance(context).putString(FinalConstants.SharedPreferencesConfig.BUYCAR_DATE,licenseDay);
    }

    public static String getInspectDate(Context context){
        return PreferencesUtls.getInstance(context).getString(FinalConstants.SharedPreferencesConfig.INSPECT_DATE,"");
    }

    public static String getBuyCarDate(Context context){
        return PreferencesUtls.getInstance(context).getString(FinalConstants.SharedPreferencesConfig.BUYCAR_DATE,"");
    }


    /**
     * 年检提示:
     *     在预定的年检日期前30,7,3天提示
     * @param context
     */
    public static void showInspectTip(Context context){
        String inspectDate = getInspectDate(context);
        if (TextUtils.isEmpty(inspectDate))
            return;
        //日期不在年检日期之前则返回
        if (!DateHelper.nowIsBefore(inspectDate))
            return;
        int days = Math.abs(DateHelper.getBetweenDays(inspectDate));
        if (days == 30 || days == 7 || days == 3){
            NotificationUtil.showNotification(context,
                    context.getResources().getString(R.string.vehicle_inspection_tip),
                    String.format(context.getResources().getString(R.string.vehicle_inspection_tip_msg),days),
                    "com.shanghaionstar.vehicle_ownerlife.VehicleInspectionWithDAActivity");
        }
    }

}
